//时间格式转换成字符串
function Format(datetime,fmt) {
    if (parseInt(datetime)==datetime) {
        if (datetime.length==10) {
            datetime=parseInt(datetime)*1000;
        } else if(datetime.length==13) {
            datetime=parseInt(datetime);
        }
    }
    datetime=new Date(datetime);
    var o = {
        "M+" : datetime.getMonth()+1,                 //月份
        "d+" : datetime.getDate(),                    //日
        "h+" : datetime.getHours(),                   //小时
        "m+" : datetime.getMinutes(),                 //分
        "s+" : datetime.getSeconds(),                 //秒
        "q+" : Math.floor((datetime.getMonth()+3)/3), //季度
        "S"  : datetime.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (datetime.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}

function myInformation() {

    $.ajax({
        url: "/webapi/login/myInformation",
    }).done(function (rs) {
        var item=rs[0];
        var html="账号:"+item.userName;
        $("#My-information").html(html);

    })
}

function giveGood(id) {
    $.ajax({
        url:"webapi/blog/article/giveGood",
        method:"post",
        data:{"id":id}
    }).done(function () {

        loadBlog();
        alert("点赞成功");

    })
}

function searchArticle() {
    var title=$("#searchArticle").val();

    $.ajax({
        url:"/webapi/blog/article/findBytTitle",
        method:"post",
        data:{
            title:title
        }
    }).done(function (rs) {
        var html="";
        var length=rs.length;
        var dateEnd = new Date();
        for(var i=0;i<length;i++){
            var item=rs[i];
            //将Date的值转化成String
            var time2=Format(item.createTime,"yyyy-MM-dd")
            var dateBegin = new Date(time2.replace(/-/g, "/"));
            var dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数
            var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));//计算出相差天数
            html+="<div class=\"panel\">\n" +
                "                                <div class=\"blog-header\">\n" +
                "                                    <img class=\"img-responsive\" src=\"/img/shared-img-"+(item.id)%6+".jpg\" alt=\"Image\">\n" +
                "                                </div>\n" +
                "                                <div class=\"blog-content\">\n" +
                "                                    <div class=\"blog-title media-block\">\n" +
                "                                        <div class=\"media-body\">\n" +"<span id=\"title-id\">"+item.id+"</span>"+
                "                                            <a href=\"detail?titleId="+item.id+"\" class=\"btn-link\" id=\"blog-title\">\n" +"<h2>"+item.title+"</h2>"+
                "                                            </a>\n" +
                "                                        </div>\n" +
                "                                    </div>\n" +
                "                                    <div class=\"blog-body\" id=\"blog-content\">\n" +
                "                                        <p>"+(item.content).substring(0,60)+"......</p>\n" +
                "                                    </div>\n" +
                "                                </div>\n" +
                "                                <div class=\"blog-footer\">\n" +
                "                                    <div class=\"media-left\">\n" +
                "                                        <span class=\"label label-success\">"+dayDiff+"天前"+"</span>\n" +
                "                                    </div>\n" +
                "                                    <div class=\"media-body text-right\">\n" +
                "                                        <span class=\"mar-rgt\"><i class=\"demo-pli-heart-2 icon-fw\""+"id=\"giveGood"+item.id+"\""+"onclick=\"giveGood(this.id)\"></i>"+item.good+"</span>\n" +
                "                                    </div>\n" +
                "                                </div>\n" +
                "                            </div>\n"
        }
        $("#real-blog").html(html);
    })
}

function addArticle() {

    var title=$("#title").val();
    var content=$("#content").val();
    $.ajax({
        url:"webapi/blog/article/insert",
        method:"post",
        data:{
            title:title,
            content:content
        }
    }).done(function () {
        loadBlog();
        $("#addArticle").modal("hide");
    })
}

function LoadRecentArticle() {
    $.ajax({
        url: "webapi/blog/article/list",
    }).done(function (rs) {

        var length=rs.length;
        var html="";

        var dateEnd = new Date();

        for(var i=0;i<length;i++){
            var item=rs[i];
            //将Date的值转化成String
            var time2=Format(item.createTime,"yyyy-MM-dd")
            var dateBegin = new Date(time2.replace(/-/g, "/"));
            var dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数
            var dayDiff = Math.floor(dateDiff / ( 3600 * 1000));//计算出相差小时数

            html=" <div class=\"list-group-item list-item-sm\">" +
                "<a href=\"detail?titleId="+item.id+"\" class=\"btn-link\">"+item.title+"</a><" +
                "small class=\"box-block\">"+dayDiff+"小时前"+"</small>" +
                "</div>"+html;

        }
        $("#recent-article-real").html(html);
    })
}

function LoadRecentComment(){
    $.ajax({
        url: "webapi/blog/comment/list",
    }).done(function (rs) {

        var length=rs.length;
        var html="";

        var dateEnd = new Date();

        for(var i=0;i<length;i++){
            var item=rs[i];
            //将Date的值转化成String
            var time2=Format(item.createCommentTime,"yyyy-MM-dd")
            var dateBegin = new Date(time2.replace(/-/g, "/"));
            var dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数
            var dayDiff = Math.floor(dateDiff / ( 3600 * 1000));//计算出相差小时数

            html=" <a href=\"#\" class=\"list-group-item\">\n" +
                "                                <div class=\"media-left pos-rel\">\n" +
                "                                    <img class=\"img-circle img-xs\" src=\"img/profile-photos/"+(item.userId)%10+".jpg\" alt=\"Profile Picture\">\n" +
                "                                </div>\n" +
                "                                <div class=\"media-body\">\n" +
                "                                    <p class=\"mar-no text-main\">"+item.userName+"</p>\n" +
                "                                    <small>"+dayDiff+"小时前"+"</small>\n" +
                "                                    <p class=\"pad-top text-sm\">"+item.comment+"</p>\n" +
                "                                </div>"+html;

        }
        $("#recent-comment-real").html(html);
    })
}


function loadBlog(){
    $.ajax({
        url: "webapi/blog/article/list",
    }).done(function (rs) {

        var length=rs.length;
        var html="";

        var dateEnd = new Date();



        for(var i=0;i<length;i++){
            var item=rs[i];
            //将Date的值转化成String
            var time2=Format(item.createTime,"yyyy-MM-dd")
            var dateBegin = new Date(time2.replace(/-/g, "/"));
            var dateDiff = dateEnd.getTime() - dateBegin.getTime();//时间差的毫秒数
            var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));//计算出相差天数
            html+="<div class=\"panel\">\n" +
                "                                <div class=\"blog-header\">\n" +
                "                                    <img class=\"img-responsive\" src=\"/img/shared-img-"+(item.id)%6+".jpg\" alt=\"Image\">\n" +
                "                                </div>\n" +
                "                                <div class=\"blog-content\">\n" +
                "                                    <div class=\"blog-title media-block\">\n" +
                "                                        <div class=\"media-body\">\n" +"<span id=\"title-id\">"+item.id+"</span>"+
                "                                            <a href=\"detail?titleId="+item.id+"\" class=\"btn-link\" id=\"blog-title\">\n" +"<h2>"+item.title+"</h2>"+
                "                                            </a>\n" +
                "                                        </div>\n" +
                "                                    </div>\n" +
                "                                    <div class=\"blog-body\" id=\"blog-content\">\n" +
                "                                        <p>"+(item.content).substring(0,60)+"......</p>\n" +
                "                                    </div>\n" +
                "                                </div>\n" +
                "                                <div class=\"blog-footer\">\n" +
                "                                    <div class=\"media-left\">\n" +
                "                                        <span class=\"label label-success\">"+dayDiff+"天前"+"</span>\n" +
                "                                    </div>\n" +
                "                                    <div class=\"media-body text-right\">\n" +
                "                                        <span class=\"mar-rgt\"><i class=\"demo-pli-heart-2 icon-fw\""+"id=\"giveGood"+item.id+"\""+"onclick=\"giveGood(this.id)\"></i>"+item.good+"</span>\n" +
                "                                    </div>\n" +
                "                                </div>\n" +
                "                            </div>\n"
        }
        $("#real-blog").html(html);
    })
}
$(function () {
    loadBlog();
    LoadRecentArticle();
    myInformation();
    LoadRecentComment();
})